﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KBManagement.DAL;
using KBManagement.Library;
using CrystalDecisions.CrystalReports;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Windows;


namespace KBManagement
{
    public partial class ReportEmployee : Form
    {
        KBSQLClassDataContext context;
        public ReportEmployee(IQueryable employeeQuery)
        {
            System.Data.DataTable myTable = new DataTable("myTable");
            System.Data.DataColumn column;
            System.Data.DataRow row;

            //create 3 columns
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "ID";
            column.ReadOnly = true;
            column.Unique = true;

            // Add the Column to the DataColumnCollection.
            myTable.Columns.Add(column);
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "FullName";
            column.ReadOnly = true;

            // Add the Column to the DataColumnCollection.
            myTable.Columns.Add(column);
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "BirthDay";
            column.ReadOnly = true;

            // Add the Column to the DataColumnCollection.
            myTable.Columns.Add(column);
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "Gender";
            column.ReadOnly = true;

            // Add the Column to the DataColumnCollection.
            myTable.Columns.Add(column);
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "Address";
            column.ReadOnly = true;

            // Add the Column to the DataColumnCollection.
            myTable.Columns.Add(column);
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "Phone";
            column.ReadOnly = true;

            // Add the Column to the DataColumnCollection.
            myTable.Columns.Add(column);
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "Email";
            column.ReadOnly = true;

            // Add the Column to the DataColumnCollection.
            myTable.Columns.Add(column);
            context = Utils.CreateInstance().GetDBContext();
            InitializeComponent();

            ReportDocument reportDocument = new ReportDocument();
            string path = Application.StartupPath;
            path = path.Substring(0, path.LastIndexOf("bin"));
            reportDocument.Load(path + "CrystalReportEmployee.rpt");

            foreach (Employee employee in employeeQuery)
            {
                row = myTable.NewRow();
                row["ID"] = employee.ID;
                row["FullName"] = employee.FullName;
                row["BirthDay"] = employee.BirthDay.ToShortDateString();
                row["Gender"] = employee.Gender;
                row["Address"] = employee.Address;
                row["Phone"] = employee.Phone;
                row["Email"] = employee.Email;
                //add the row of data to the table
                myTable.Rows.Add(row);
            }
            reportDocument.SetDataSource(myTable);
            crystalReportViewer1.ReportSource = reportDocument;
        }
    }
}
